package com.xbongbong.formula.qlexpress.operator.text;

import com.alibaba.fastjson.JSON;
import com.ql.util.express.Operator;
import com.xbongbong.formula.constant.Constant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BusinessRuleErrorCodeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/7/5 15:22
 * @desrcption: 将多个数据转化为list。
 */
public class OperatorJoin extends Operator {

    public OperatorJoin(String name){
        this.name = name;
    }

    public OperatorJoin(String aliasName, String aName, String errorInfo){
        this.aliasName = aliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }

    /**
     * @param list
     * list[] 将多个数据转化为字符串。
     * @return
     * @throws Exception
     */
    @Override
    public Object executeInner(Object[] list) throws Exception {
        if (Objects.isNull(list)) {
            throw new XbbException(BusinessRuleErrorCodeEnum.API_ERROR_261007);
        }
        if (list[0].getClass().isArray() || list[0] instanceof List) {
            String split = "、";
            List<String> valueList = JSON.parseArray(JSON.toJSONString(list[0]), String.class);
            if (list.length == Constant.LENGTH) {
                split = list[1].toString();
            }
            return StringUtils.join(valueList, split);
        }
        return list[0];

    }
}

